<?php
/**
 * 
 * Attachment(附件管理)
 */
if (! defined ( "IICCMS" ))
	exit ( "Access Denied" );
class AttachmentAction extends Action {
	protected $dao, $Config, $sysConfig, $isadmin = 0, $userid = 0, $groupid = 0;
	function _initialize() {
		//F("sess",$_POST);
		$this->Config = F ( 'Config' );
		$this->sysConfig = F ( 'sys.config' );
		C ( 'ADMIN_ACCESS', $this->sysConfig ['ADMIN_ACCESS'] );
		if ($_POST ['PHPSESSID'] && $_POST ['swf_auth_key'] && ($_POST ['swf_auth_key'] == sysmd5 ( $_POST ['PHPSESSID'] . $_POST ['userid'] ))) {
			$this->userid = $_POST ['userid'];
		}
		$this->isadmin = $_REQUEST ['isadmin'] ? $_REQUEST ['isadmin'] : 0;
		if (!$this->isadmin) {
			if ($_COOKIE ['iic_auth']) {
				if (! strstr ( $_SERVER ['HTTP_USER_AGENT'], 'Flash' ))
					cookie ( 'iic_cookie', $_SERVER ['HTTP_USER_AGENT'] );
				$HTTP_USER_AGENT = strstr ( $_SERVER ['HTTP_USER_AGENT'], 'Flash' ) ? $_COOKIE ['iic_cookie'] : $_SERVER ['HTTP_USER_AGENT'];
				$yourphp_auth_key = sysmd5 ( $this->sysConfig ['ADMIN_ACCESS'] . $HTTP_USER_AGENT );
				list ( $userid, $groupid, $password ) = explode ( "-", authcode ( $_COOKIE ['iic_auth'], 'DECODE', $yourphp_auth_key ) );
				$this->userid = $userid;
				$this->groupid = $groupid;
			}
			if (! $this->userid) {
				$this->assign ( 'jumpUrl', U ( 'User-Login/index' ) );
				$this->error ( L ( 'no_login' ) );
			}
		}
		$this->dao = M ('Attachment');
		//F('sess',$this->dao);
	}
	
	public function index() {
		$auth = str_replace ( ' ', '+', $_REQUEST ['auth'] );
		//dump($auth);
		unset ( $_REQUEST ['auth'] );
		$upsetup = implode ( '-', $_REQUEST );
		//dump($upsetup);
		$yourphp_auth_key = sysmd5 ( C ( 'ADMIN_ACCESS' ) . $_SERVER ['HTTP_USER_AGENT'] );
		$enupsetup = authcode ( $auth, 'DECODE', $yourphp_auth_key );
		//		dump($upsetup);
		//		dump($enupsetup);
		if (! $enupsetup || $upsetup != $enupsetup)
			$this->error ( L ( 'do_empty' ) );
		
		$sessid = time ();
		
		$count = $this->dao->where ( 'status=0 and userid =' . $this->userid )->count ();
		$this->assign ( 'no_use_files', $count );
		
		$types = '*.' . str_replace ( ",", ";*.", $_REQUEST ['file_types'] );
		$this->assign ( 'moduleid', $_REQUEST ['moduleid'] );
		$this->assign ( 'file_size', $_REQUEST ['file_size'] );
		$this->assign ( 'file_limit', $_REQUEST ['file_limit'] );
		$this->assign ( 'file_types', $types );
		$this->assign ( 'isthumb', $_REQUEST ['isthumb'] );
		$this->assign ( 'isadmin', $this->isadmin );
		$this->assign ( 'sessid', $sessid );
		$this->assign ( 'userid', $this->userid );
		$swf_auth_key = sysmd5 ( $sessid . $this->userid );
//		dump ($sessid.$this->userid);
//		dump(sysmd5 ( $sessid . $this->userid ));
		$this->assign ( 'swf_auth_key', $swf_auth_key );
		$this->assign ( 'more', $_GET ['more'] );
		$this->display ();
	}
	
	public function upload() {
//		$returndata=array();
//		$this->ajaxReturn ( $returndata, L ( 'upload_ok' ), '1' );
		if ($_POST ['swf_auth_key'] != sysmd5 ( $_POST ['PHPSESSID'] . $this->userid ))
			$this->ajaxReturn ( 0, L ( 'do_empty' ), 0 );
		
		//iic
		$md5file = md5_file ( $_FILES ['filedata'] ['tmp_name'] );
		//F('sess',$md5file);
		$dao = M ( "Attachment" );
		$condition = array ("hash" => $md5file );
		$info = array ();
		$info = $dao->where ( $condition )->find ();
		if ($info) {
			$returndata ['aid'] = $info ['aid'];
			$returndata ['filepath'] = __APP__ . '/p/' . $info ['short'] . '.' . $info ['fileext'];
			$returndata ['fileext'] = $info ['fileext'];
			$returndata ['isimage'] = $info ['isimage'];
			$returndata ['filename'] = $info ['filename'];
			$returndata ['filesize'] = $info ['filesize'];
			$this->ajaxReturn ( $returndata, L ( 'upload_ok' ), '1' );
		}
		//iic
		import ( "@.ORG.UploadFile" );
		$upload = new UploadFile ();
		//F("sess",$upload);
		//$upload->supportMulti = false;
		//设置上传文件大小 
		$upload->maxSize = $this->Config ['attach_maxsize'];
		$upload->autoSub = true;
		$upload->subType = 'date';
		$upload->dateFormat = 'Ym';
		//设置上传文件类型 
		$upload->allowExts = explode ( ',', $this->Config ['attach_allowext'] );
		//设置附件上传目录 
		$upload->savePath = UPLOAD_PATH;
		//设置上传文件规则 
		$upload->saveRule = uniqid;
		
		//删除原图 
		$upload->thumbRemoveOrigin = true;
		if (! $upload->upload ()) {
			$this->ajaxReturn ( 0, $upload->getErrorMsg (), 0 );
		} else {
			//取得成功上传的文件信息 
			$uploadList = $upload->getUploadFileInfo ();
			
			if ($_POST ['addwater']) {
				import ( "@.ORG.Image" );
				Image::watermark ( $uploadList [0] ['savepath'] . $uploadList [0] ['savename'], '', $this->Config );
			}
			
			$imagearr = explode ( ',', 'jpg,gif,png,jpeg,bmp,ttf,tif' );
			$data = array ();
			$model = M ( 'Attachment' );
			//保存当前数据对象
			$data ['moduleid'] = $_REQUEST ['moduleid'];
			$data ['catid'] = 0;
			$data ['userid'] = $_REQUEST ['userid'];
			$data ['filename'] = $uploadList [0] ['name'];
			//iic
			$data ['hash'] = $md5file;
			$data ['filepath'] = $uploadList [0] ['savepath'];
			$data ['fname'] = $uploadList [0] ['savename'];
			$shot = shorturl ( $md5file );
			$data ['short'] = $shot;
			//iic
			//$data['filepath'] = $uploadList[0]['savepath'].$uploadList[0]['savename'];
			$data ['filesize'] = $uploadList [0] ['size'];
			$data ['fileext'] = $uploadList [0] ['extension'];
			$data ['isimage'] = in_array ( $uploadList [0] ['extension'], $imagearr ) ? 1 : 0;
			$data ['isthumb'] = intval ( $_REQUEST ['isthumb'] );
			$data ['createtime'] = time ();
			$data ['uploadip'] = get_client_ip ();
			$aid = $model->add ( $data );
			$returndata ['aid'] = $aid;
			$returndata ['filepath'] = __APP__ . '/p/' . $data ['short'] . '.' . $data ['fileext'];
			//$returndata['filepath'] = $data['filepath'];
			$returndata ['fileext'] = strtolower ( $data ['fileext'] );
			$returndata ['isimage'] = $data ['isimage'];
			$returndata ['filename'] = $data ['filename'];
			$returndata ['filesize'] = $data ['filesize'];
			
			$this->ajaxReturn ( $returndata, L ( 'upload_ok' ), '1' );
		
		//print_r($uploadList[0]['savepath'].$uploadList[0]['savename']);
		}
	}
	
	public function filelist() {
		
		$where = $_REQUEST ['typeid'] ? " status=1 " : " status=0 ";
		$where .= " and userid = " . $this->userid;
		import ( '@.ORG.Page' );
		$count = $this->dao->where ( $where )->count ();
		$page = new Page ( $count, 12 );
		$imagearr = explode ( ',', 'jpg,gif,png,jpeg,bmp,ttf,tif' );
		
		$page->urlrule = 'javascript:ajaxload(' . $_REQUEST ['typeid'] . ',{$page},\'' . $_REQUEST ['inputid'] . '\',' . $this->isadmin . ');';
		$show = $page->show ();
		$this->assign ( "page", $show );
		$list = $this->dao->order ( 'aid desc' )->where ( $where )->limit ( $page->firstRow . ',' . $page->listRows )->select ();
		foreach ( ( array ) $list as $key => $r ) {
			$list [$key] ['filepath'] = __APP__ . '/p/' . $r ['short'] . '.' . $r ['fileext'];
			$list [$key] ['thumb'] = in_array ( $r ['fileext'], $imagearr ) ? $list [$key] ['filepath'] : __ROOT__ . '/Public/Images/ext/' . $r ['fileext'] . '.png';
		}
		$this->assign ( 'list', $list );
		$this->display ();
	}
	
	function delfile($aid) {
		if (empty ( $aid )) {
			$aid = $_REQUEST ['aid'];
		}
		$r = delattach ( array ('aid' => $aid, 'userid' => $this->userid ) );
		if ($r) {
			$this->success ( L ( 'delete_ok' ) );
		} else {
			$this->error ( L ( 'delete_error' ) );
		}
	
	}
	function cleanfile() {
		
		$r = delattach ( array ('status' => 0, 'userid' => $this->userid ) );
		if ($r) {
			$this->success ( L ( 'delete_ok' ) );
		} else {
			$this->error ( L ( 'delete_error' ) );
		}
	}

}
?>